const fs = require('fs');

function _makeDir (curdir, list){
  if (!curdir) return;
  try {fs.mkdirSync(curdir);} catch (e) {};
  let now = list.shift();
  now && _makeDir(curdir + "/" + now, list)
};
function makeDir (dir) {
  if (!dir) return;
  const list = dir.split("/");
  const now = list.shift();
  _makeDir(now, list)
}

module.exports = (res, urlObj, bodyObj, sql) => {
  const path = `./serve/${bodyObj.type === "min" ? "lolmin" : "lol"}/` + bodyObj.src;
  const pathArr = path.split("/");
  pathArr.pop();
  makeDir(pathArr.join("/"));
  fs.writeFileSync(path, new Buffer(bodyObj.data.replace(/^data.+base64,/, ""), 'base64'));
  res.end(
    JSON.stringify({
      code: 0,
      msg: "请求成功",
    })
  );
}
